# -*- coding: utf-8 -*-
import scrapy
import json
import time
from PandaTV_Star.items import PandatvStarItem

class StarFaceSpider(scrapy.Spider):
    name = 'star_face'
    allowed_domains = ['panda.tv']


    pageno = 1

    # 一次返回数量 (最大30)
    pagenum=5
    # 时间戳
    now = int(time.time())
    total = 0

    # 拼接地址
    url = "https://m.api.xingyan.panda.tv/room/list?guid=DDA07566-9D1E-4DC1-8550-184890088405&__plat=ios&__version=3.1.17.4630&__channel=appstore&banner=1&pageno="+str(pageno)+"&pagenum="+str(pagenum)+"&pt_sign=5331828d74643222d7c7a2abf7bb29f9&pt_time=" + str(now)

    start_urls = [url]

    def parse(self, response):
        # 转JSON
        jsonData = json.loads(response.text)["data"]
        # 保存总数
        self.total = jsonData["total"]
        for each in jsonData["items"]:
            item = PandatvStarItem()
            item["name"] = each["name"]
            photo : str = each["s_photo"]
            # 替换高质量图片
            item["hd_pic"] = photo.replace('image/resize,w_480/quality,q_70', 'image/resize,w_960/quality,q_100')
            item["stream_url"] = each["streamurl"]
            yield item
        
        self.pageno += 1
        print("-------------------------------------->"+self.url)
        if self.pageno * self.pagenum < self.total:
            print("-"*30 + "下一页")
            yield scrapy.Request(url="https://m.api.xingyan.panda.tv/room/list?guid=DDA07566-9D1E-4DC1-8550-184890088405&__plat=ios&__version=3.1.17.4630&__channel=appstore&banner=1&pageno="+str(self.pageno)+"&pagenum="+str(self.pagenum)+"&pt_sign=5331828d74643222d7c7a2abf7bb29f9&pt_time=" + str(self.now), callback=self.parse)
